package com.hsd.znsh.controller.manager;

import com.hsd.znsh.entity.Sensor;
import com.hsd.znsh.entity.SensorGroup;
import com.hsd.znsh.entity.SensorType;
import com.hsd.znsh.enums.DeletedStatusEnum;
import com.hsd.znsh.enums.SensorGroupTypeEnum;
import com.hsd.znsh.pojo.output.*;
import com.hsd.znsh.pojo.query.SensorGroupQuery;
import com.hsd.znsh.pojo.query.SensorQuery;
import com.hsd.znsh.service.SensorGroupService;
import com.hsd.znsh.service.SensorService;
import com.hsd.znsh.service.SensorTypeService;
import com.hsd.znsh.utils.EnumUtil;
import com.hsd.znsh.utils.ResultVOUtil;
import com.hsd.znsh.utils.SecurityUtil;
import com.hsd.znsh.utils.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/*
 * @auther: wangjiacai
 *
 * @create: 2018-04-25 14:47:03
 */
@RestController
@RequestMapping("/manager/sensor")
public class ManagerSensorController {

    @Autowired
    private SensorService sensorService;

    @Autowired
    private SensorTypeService sensorTypeService;

    @Autowired
    private SensorGroupService sensorGroupService;

    @GetMapping("/getSensorGroupTypeList")
    public ResultVO getSensorGroupTypeList(){
        List<Map<String,Object>> list= EnumUtil.getEnumInfo(SensorGroupTypeEnum.class,"sensorGroupType");
        return ResultVOUtil.success(list);
    }

    @PostMapping("/getSensorGroupList")
    public ResultVO getSensorGroupList(@PageableDefault(sort = "updateDateTime" ,direction = Sort.Direction.DESC) Pageable pageable, SensorGroupQuery sensorGroupQuery){
        List<SensorGroupVO> list;
        if(SecurityUtil.hasRole("ROLE_ROOT")){
            list=sensorGroupService.getSensorGroupList(pageable,sensorGroupQuery,null);
        }else{
            list=sensorGroupService.getSensorGroupList(pageable,sensorGroupQuery,SecurityUtil.getUserId());
        }
        return ResultVOUtil.success(list);
    }

    @GetMapping("/getSensorGroupList")
    public ResultVO getSensorGroupListTotalPages(Integer size, SensorGroupQuery sensorGroupQuery){
        long totalNum;
        if(SecurityUtil.hasRole("ROLE_ROOT")){
            totalNum=sensorGroupService.getSensorGroupListTotalPages(sensorGroupQuery,null);
        }else{
            totalNum=sensorGroupService.getSensorGroupListTotalPages(sensorGroupQuery,SecurityUtil.getUserId());
        }
        return ResultVOUtil.success((long)Math.ceil(totalNum/(double)size));
    }

    @GetMapping("/getSensorGroupInfo")
    public ResultVO getSensorGroupInfo(Integer id){
        SensorGroupVO sensorGroupVO=sensorGroupService.getSensorGroupInfo(id);
        return ResultVOUtil.success(sensorGroupVO);
    }

    @PostMapping("/editSensorGroup")
    public ResultVO editSensorGroup(@Valid SensorGroup sensorGroup,BindingResult bindingResult){
        ValidatorUtil.valid(bindingResult);
        if(sensorGroup.getId()==null){
            sensorGroupService.addSensorGroup(sensorGroup);
            return ResultVOUtil.success("添加成功");
        }else{
            sensorGroupService.updateSensorGroup( sensorGroup);
            return ResultVOUtil.success("修改成功");
        }
    }

    @PostMapping("/deleteSensorGroup")
    public ResultVO deleteSensorGroup(Integer id){
        sensorGroupService.deleteSensorGroup(id);
        return ResultVOUtil.success("删除成功");
    }

    @GetMapping("/getAllSensorTypeList")
    public ResultVO getAllSensorTypeList(){
        List<SensorType> list=sensorTypeService.getAllSensorTypeList();
        return ResultVOUtil.success(list);
    }

    @GetMapping("/getSensorList")
    public ResultVO getSensorListTotalPages(SensorQuery sensorQuery, Integer size){
        long totalNum;
        if(SecurityUtil.hasRole("ROLE_ROOT")){
            totalNum=sensorService.getSensorListTotalPages(sensorQuery,null);
        }else{
            totalNum=sensorService.getSensorListTotalPages(sensorQuery,SecurityUtil.getUserId());
        }
        return ResultVOUtil.success((long)Math.ceil(totalNum/(double)size));
    }

    @PostMapping("/getSensorList")
    public ResultVO getSensorList(SensorQuery sensorQuery,Pageable pageable){
        List<SensorVO> list;
        if(SecurityUtil.hasRole("ROLE_ROOT")){
            list=sensorService.getSensorList(sensorQuery,pageable,null);
        }else{
            list=sensorService.getSensorList(sensorQuery,pageable,SecurityUtil.getUserId());
        }
        return ResultVOUtil.success(list);
    }

    @GetMapping("/getSensorInfo")
    public ResultVO getSensorInfo(Integer id){
        SensorVO sensorvo=sensorService.getSensorInfo(id);
        return ResultVOUtil.success(sensorvo);
    }

    @PostMapping("/editSensor")
    public ResultVO editSensor(@Valid Sensor sensor,BindingResult bindingResult){
        ValidatorUtil.valid(bindingResult);
        if(sensor.getId()==null){
            sensorService.addSensor(sensor);
            return ResultVOUtil.success("添加成功");
        }else{
            sensorService.updateSensor(sensor);
            return ResultVOUtil.success("修改成功");
        }
    }

    @PostMapping("/deleteSensor")
    public ResultVO deleteSensor(Integer id){
        sensorService.deleteSensor(id);
        return ResultVOUtil.success("删除成功");
    }
}
